package com.education.welco.controller;

import com.education.auth.entity.UserInfo;
import com.education.common.pojo.AjaxResult;
import com.education.utils.JwtUtil;
import com.education.websocket.data.HomeData;
import com.education.websocket.server.WebSocketServerWELCO;
import com.education.welco.pojo.MessageNotification;
import com.education.welco.service.IMessageNotificationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 消息通知 controller业务
 *
 * @Author: liftsail
 * @Date: 2022-10-17
 * @Description: 不积跬步无以至千里
 */
@RestController
@RequestMapping("/messageNotification")
public class MessageNotificationController {

    @Autowired
    private IMessageNotificationService messageNotificationService;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 列表查询消息通知
     */
    @GetMapping("/list")
    public AjaxResult list(MessageNotification messageNotification, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        //查询消息列表:返回未读和已读消息，排序。点击详情接口后实现已读状态修改
        List<MessageNotification> list = messageNotificationService.selectMessageNotificationList(messageNotification);

        PageInfo pageInfo = new PageInfo<>(list);
        return AjaxResult.success("查询成功！", pageInfo);
    }


    /**
     * 新增保存消息通知
     */
    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody MessageNotification messageNotification) {
        messageNotificationService.insertMessageNotification(messageNotification);
        return AjaxResult.success("添加成功！");
    }


    /**
     * 主键详情消息通知
     */
    @GetMapping("/selectOne")
    public AjaxResult selectOne(Integer id) {
        MessageNotification result = messageNotificationService.selectMessageNotificationById(id);
        return AjaxResult.success("查询成功！", result);
    }

    /**
     * 修改保存消息通知
     */
    @PostMapping("/update")
    public AjaxResult editSave(@RequestBody MessageNotification messageNotification) {
        messageNotificationService.updateMessageNotification(messageNotification);
        return AjaxResult.success("修改成功！");
    }

    /**
     * 修改消息已读状态&&websocket的数量减1
     * messageId 消息id
     * isRead 0未读 1已读
     */
    @PostMapping("/updateState2websocket")
    public AjaxResult updateState2websocket(@RequestBody MessageNotification messageNotification) {
        messageNotificationService.updateMessageNotification(messageNotification);
        //websocket的消息已读redis数量更新依据分页查询的数量
        UserInfo userInfo = jwtUtil.getInfoFromToken();
        Long teaUserId = userInfo.getId();
        Object res = redisTemplate.opsForValue().get("wait:data:" + teaUserId);
        if (Objects.nonNull(res)) {
            HomeData homeData = (HomeData) res;
            if (homeData.getMessageWait() > 0) {
                homeData.setMessageWait(homeData.getMessageWait() - 1);
            }
            redisTemplate.opsForValue().set("wait:data:" + teaUserId, homeData);
            WebSocketServerWELCO.sendInfo(homeData, String.valueOf(teaUserId));
        } else {
            HomeData homeData = new HomeData();
            homeData.setMessageWait(0);
            redisTemplate.opsForValue().set("wait:data:" + teaUserId, homeData);
            WebSocketServerWELCO.sendInfo(homeData, String.valueOf(teaUserId));
        }
        //更改已阅读消息的状态
        //if (size > 0) {
        //    List<Integer> messageIdList = list.parallelStream().map(messageNotification1 -> messageNotification1.getMessageId()).collect(Collectors.toList());
        //    int i = messageNotificationService.batchUpdateReadState(messageIdList);
        //}
        return AjaxResult.success("状态修改成功！");
    }

    /**
     * 删除消息通知
     */
    @PostMapping("/del")
    public AjaxResult remove(String ids) {
        messageNotificationService.deleteMessageNotificationByIds(ids);
        return AjaxResult.success("删除成功！");
    }
}